<?php
###############   COPYLEFT GPLv3 LICENSE   ###############
##
## JFX Version 0.2.7
## Website Management Software
## www.jfxcms.com
##
## Copyright 2009 GPLv3 - http://www.opensource.org/licenses/gpl-3.0.html
##
## Anthony Gallon
## oi_antz@hotmail.com
##
## Permission is hereby granted to any person having a copy of this software
## to freely use and modify as required so long as the copyright notices
## and branding remain intact.
##
## Full license details available at http://www.jfxcms.com/license
##
###############   COPYLEFT GPLv3 LICENSE   ###############

$showMemoryUsage = true;

JFX::checkAdminPermsRedirect('core', 'perm_create_page');

$THEME->addBreadcrumb($this->adminUrl.'/new-page/', $this->lang('ap_title_new-page'));

$VIEW->template = 'admin-edit-page.tpl';


// generate a new page id
$defaultDomain = $DB->oneValue($CONFIG->dbprefix.'domains', 'id', "is_default = 1");
$parentid = (int) $DB->oneValue($CONFIG->dbprefix.'pages', 'id', "parent_id = 0 AND domain_id = '{$defaultDomain}' AND workspace_id = '{$USER->workspace}'");

$slug = 'new-page';
while($DB->countRows($CONFIG->dbprefix.'pages', "slug = '{$slug}'")>0){
    $slug .= '1';
}
$dbParams = array(
    'id' => '',
    'parent_id' => $parentid,
    'slug' => $slug,
    'is_published' => 0,
    'workspace_id' => $USER->workspace,
    'owner_id' => $USER->userid,
    'created_tstamp' => time(),
    'modified_tstamp' => 0,
    'domain_id' => $CONFIG->domainid,
    'is_default' => 0
);
$pageid = $DB->insert($CONFIG->dbprefix.'pages', $dbParams);

$LANG->updateContent('core', 'page_title', $pageid, 'New Page');
$LANG->updateContent('core', 'page_menu', $pageid, 'New Page');
$LANG->updateContent('core', 'page_content', $pageid, '');

// create mount permission for the user
$dbParams = array(
    'user_id' => $USER->userid,
    'module_key' => 'core',
    'permission_key' => 'perm_page_'.$USER->workspace.'_'.$pageid,
    'workspace_id' => $USER->workspace
);
$DB->insert($CONFIG->dbprefix.'admin_users_to_permissions', $dbParams);



$dbParams['id'] = $pageid;
JFX::hook('JFX_Core.newPage', $dbParams);






JFX::redirect($CONFIG->adminUrl.'/edit-page/?page='.$pageid.'&showedit=1');



echo '<div class="jfx-droppable jfx-page" id="jfxpage-'.$pageid.'"></div>';

$VIEW->addJs('$(document).ready(function(){
     AdminBlocker.init(\''.$pageid.'\', \''.$USER->workspace.'\');
     tb_show(\'Editing Page Details\', \''.$CONFIG->adminUrl.'/ajax/edit-page?page='.$pageid.'&KeepThis=true&TB_iframe=true&height=500&width=700\', false);
     $("#TB_window").unload(function(){
        window.location = "'.$CONFIG->adminUrl.'/edit-page/?page='.$pageid.'";
    });
});');




